/*
 * Copyright (C) 2017 zhouyou(478319399@qq.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.shuyihui.rxhttp.callback

import android.app.Dialog
import com.shuyihui.rxhttp.exception.ApiException
import com.shuyihui.rxhttp.subsciber.IProgressDialog
import com.shuyihui.rxhttp.subsciber.ProgressCancelListener
import io.reactivex.disposables.Disposable

/**
 *
 * 描述：可以自定义带有加载进度框的回调
 * 1.可以自定义带有加载进度框的回调,是否需要显示，是否可以取消<br></br>
 * 2.取消对话框会自动取消掉网络请求<br></br>
 */
abstract class ProgressDialogCallBack<T> : CallBack<T>, ProgressCancelListener {
    private var progressDialog: IProgressDialog?
    private var mDialog: Dialog? = null
    private var isShowProgress = true

    constructor(progressDialog: IProgressDialog?) {
        this.progressDialog = progressDialog
        init(false)
    }

    /**
     * 自定义加载进度框,可以设置是否显示弹出框，是否可以取消
     *
     * @param progressDialog dialog
     * @param isShowProgress 是否显示进度
     * @param isCancel       对话框是否可以取消
     */
    constructor(progressDialog: IProgressDialog?, isShowProgress: Boolean, isCancel: Boolean) {
        this.progressDialog = progressDialog
        this.isShowProgress = isShowProgress
        init(isCancel)
    }

    /**
     * 初始化
     *
     * @param isCancel
     */
    private fun init(isCancel: Boolean) {
        if (progressDialog == null) return
        mDialog = progressDialog!!.dialog
        if (mDialog == null) return
        mDialog!!.setCancelable(isCancel)
        if (isCancel) {
            mDialog!!.setOnCancelListener { onCancelProgress() }
        }
    }

    /**
     * 展示进度框
     */
    private fun showProgress() {
        if (!isShowProgress) {
            return
        }
        if (mDialog != null) {
            if (!mDialog!!.isShowing) {
                mDialog!!.show()
            }
        }
    }

    /**
     * 取消进度框
     */
    private fun dismissProgress() {
        if (!isShowProgress) {
            return
        }
        if (mDialog != null) {
            if (mDialog!!.isShowing) {
                mDialog!!.dismiss()
            }
        }
    }

    override fun onStart() {
        showProgress()
    }

    override fun onCompleted() {
        dismissProgress()
    }

    override fun onError(e: ApiException?) {
        dismissProgress()
    }

    override fun onCancelProgress() {
        if (disposed != null && !disposed!!.isDisposed) {
            disposed!!.dispose()
        }
    }

    private var disposed: Disposable? = null
    fun subscription(disposed: Disposable?) {
        this.disposed = disposed
    }
}